package autoFile.core.strategy;

import autoFile.core.strategy.interfaces.CreateFileStrategyInterface;
import com.google.common.base.CaseFormat;

import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * Created by IntelliJ IDEA.
 * User: liuliwei
 * Date: 2017-08-16
 * Time: 下午5:40
 * Description: ${description}
 */
public class CreateUserServiceFileStrategy extends AbstractStrategy implements CreateFileStrategyInterface {
    @Override
    public void createFile(Class clazz) throws IOException {
        String userServiceTemplate = getUserServiceTemplate();
        String userServiceMethodTemplate = getUserServiceMethodTemplate();
        String className = clazz.getSimpleName(); //类名称
        String classNameSmall = CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.LOWER_CAMEL).convert(className); //类名称小写
        String classNameWithService = new String(className); //类名称后面有Service
        String classNameWithServiceSmall = CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.LOWER_CAMEL).convert(classNameWithService); //类名称小写后面有Service
        String targetClassName = classNameWithService;//生成文件名称

        if (className.contains("Service")) {
            className = className.replaceFirst("Service", "");
            classNameSmall = classNameSmall.replaceFirst("Service", "");
        }
        String userCommandMethodTemplates = "";
        for (Method method : getPublicMethod(clazz)) {
            String parameterTypeAndName = getParameterTypeAndName(method);
            String finalParameterTypeAndName = getFinalParameterTypeAndName(method);
            String parameterNames = getParameterNames(method);
            String dbConstant = getDbConstant(className, method);
            String classNameT = userServiceMethodTemplate.replace("finalParameters", finalParameterTypeAndName);
            String methodName = classNameT.replace("methodName", method.getName());
            String parameters = methodName.replace("parameters", parameterTypeAndName);
            String parameterNames1 = parameters.replace("parameterNames", parameterNames);
            String result = parameterNames1.replace("dbConstant", dbConstant);
            result += "\t";
            userCommandMethodTemplates += result;
//            createGongRpcConstants(className, method, gongRpcConstantsWriter);
        }
        String replaceContent = userServiceTemplate.replace("#", userCommandMethodTemplates);
        String replace$ = replaceContent.replace("$", targetClassName);
        String fileName = "/Users/liuliwei/autoFile/" + targetClassName + ".java";
        FileWriter userCommandProxyWriter = new FileWriter(fileName, true);
        System.out.println("生成"+fileName);
        userCommandProxyWriter.write(replace$);
        userCommandProxyWriter.close();
    }

    /**
     * gas
     * service模板
     *
     * @return
     * @throws IOException
     */
    private static String getUserServiceTemplate() throws IOException {
        return getTemplateByFullPath("/Users/liuliwei/autoFile/template/userServiceTemplate");
    }

    /**
     * 得到gas Service 方法模板
     *
     * @return
     * @throws IOException
     */
    private static String getUserServiceMethodTemplate() throws IOException {
        return getTemplateByFullPath("/Users/liuliwei/autoFile/template/userServiceMethodTemplate");
    }


    public static void main(String[] args) throws IOException {
//        System.out.println(getTemplateByFullPath("template/dbs"));
        System.out.println(getTemplateByFullPath("autoFile/template/dbs/classTemplate"));
    }
}
